package com.sxx.beautifulcity.utils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.sxx.beautifulcity.application.ProjectApplication;

import org.json.JSONObject;

/**
 * Created by dllo on 15/10/21.
 * Volley请求
 */
public class VolleySingleton {

    private static VolleySingleton singleton;
    private RequestQueue queue;


    public VolleySingleton() {
        queue = getQueue();
    }

    public RequestQueue getQueue(){
        if (queue == null){
            queue = Volley.newRequestQueue(ProjectApplication.getContext());
        }
         return queue;
    }

    public static VolleySingleton getInstance(){
        if (singleton == null){
            synchronized (VolleySingleton.class){
            singleton = new VolleySingleton();
            }
        }
        return  singleton;
    }
    public static final String TAG = "VolleySingleton";

    public <T> void addRequest(Request<T> request){
        request.setTag(TAG);
        queue.add(request);
    }

    public <T> void addRequest(Request<T> request, Object tag){
        request.setTag(tag);
        queue.add(request);
    }
//
//    public void getJsonObjectRequest(
//            String url , , Response.Listener listener , Response.ErrorListener error){
//        JsonObjectRequest objectRequest = new JsonObjectRequest(url , null ,listener , error);
//        addRequest(objectRequest);
//    }


    public void removeRequest(Object tag){
        queue.cancelAll(tag);
    }


}
